

package com.kimo.shop.admin.controller;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.kimo.shop.bean.model.ProdComm;
import com.kimo.shop.common.annotation.SysLog;
import com.kimo.shop.common.util.PrincipalUtil;
import com.kimo.shop.service.ProdCommService;

import javax.validation.Valid;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.kimo.shop.common.util.PageParam;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;

/**
 * 商品评论
 *
 * @author kimo-taozhouchuan
 * @date 2019-04-19 10:43:57
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/prod/prodComm" )
public class ProdCommController {

    @Value("${kimo.expose.permission:}")
    private Boolean permission;

    private final ProdCommService prodCommService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param prodComm 商品评论
     * @return 分页数据
     */
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('prod:prodComm:page')" )
    public ResponseEntity<IPage<ProdComm>> getProdCommPage(PageParam page, ProdComm prodComm) {
        IPage<ProdComm> prodCommPage = prodCommService.getProdCommPage(page, prodComm);
        if (BooleanUtil.isFalse(permission)) {
            for (ProdComm record : prodCommPage.getRecords()) {
                if (Objects.nonNull(record.getNickName()) && PrincipalUtil.isMobile(record.getNickName())){
                    record.setNickName(PhoneUtil.hideBetween(record.getNickName()).toString());
                }
            }
        }
        return ResponseEntity.ok(prodCommPage);
    }


    /**
     * 通过id查询商品评论
     * @param prodCommId id
     * @return 单个数据
     */
    @GetMapping("/info/{prodCommId}" )
    @PreAuthorize("@pms.hasPermission('prod:prodComm:info')" )
    public ResponseEntity<ProdComm> getById(@PathVariable("prodCommId" ) Long prodCommId) {
        ProdComm prodComm = prodCommService.getById(prodCommId);
        if (!StrUtil.isBlank(prodComm.getPics())){
            prodComm.setPicsArray(prodComm.getPics().split(","));
        }
        return ResponseEntity.ok(prodComm);
    }

    /**
     * 新增商品评论
     * @param prodComm 商品评论
     * @return 是否新增成功
     */
    @SysLog("新增商品评论" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('prod:prodComm:save')" )
    public ResponseEntity<Boolean> save(@RequestBody @Valid ProdComm prodComm) {
        return ResponseEntity.ok(prodCommService.save(prodComm));
    }

    /**
     * 修改商品评论
     * @param prodComm 商品评论
     * @return 是否修改成功
     */
    @SysLog("修改商品评论" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('prod:prodComm:update')" )
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid ProdComm prodComm) {
        prodComm.setReplyTime(new Date());
        return ResponseEntity.ok(prodCommService.updateById(prodComm));
    }

    /**
     * 通过id删除商品评论
     * @param prodCommId id
     * @return 是否删除成功
     */
    @SysLog("删除商品评论" )
    @DeleteMapping("/{prodCommId}" )
    @PreAuthorize("@pms.hasPermission('prod:prodComm:delete')" )
    public ResponseEntity<Boolean> removeById(@PathVariable Long prodCommId) {
        return ResponseEntity.ok(prodCommService.removeById(prodCommId));
    }

}
